package com.sqs.controller;

import com.sqs.constant.ExceptionConstant;
import com.sqs.constant.ResultConstant;
import com.sqs.result.Result;
import com.sqs.service.CommonService;
import com.sqs.vo.CaptchaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {

    @Autowired
    private CommonService commonService;

    /**
     * 生成随机验证码
     * @return
     * */
    @GetMapping("/captcha")
    public Result<CaptchaVO> Captcha(){
        log.info("随机生成验证码");
        return Result.success(ResultConstant.CREATE_CAPTCHA_SUCCESS, commonService.createCaptcha());
    }

    /**
     * 测试云服务
     * @return
     * */
    @PostMapping("/test/{num}")
    public Result test(@PathVariable Integer num){
        if(num == 1){
            return Result.error(ExceptionConstant.CODE_400, "现在测试的是返回错误的结果");
        }
        return Result.success("现在测试的是返回成功的结果");
    }

    /**
     * 上传json文件
     * @param file
     * @return
     * */
    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        log.info("上传文件：{}", file);
        String upload = commonService.upload(file);
        return Result.success(ResultConstant.UPLOAD_SUCCESS, upload);
    }
}
